package com.redoor.iot.controller;


import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.redoor.iot.aspect.Log;
import com.redoor.iot.model.Result;
import com.redoor.iot.model.Role;
import com.redoor.iot.model.User;
import com.redoor.iot.service.IRoleService;
import com.redoor.iot.service.IUserService;
import com.redoor.iot.utils.PageUtils;
import com.redoor.iot.utils.Query;
import com.redoor.iot.utils.ResultUtil;

@RestController
@RequestMapping(value="/user")
public class UserController extends BaseController{
	
	private String prefix="user"  ;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IRoleService roleService;
	
	private PasswordEncoder passwordEncoder;
	
	
	@Log("查询用户列表")
	@GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<User> sysUserList = userService.list(query);
		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(sysUserList, total);
		return pageUtil;
	}
	
	@Log("添加用户")
	@GetMapping("/add")
	String add(Model model) {
		List<Role> roles = roleService.list();
		model.addAttribute("roles", roles);
		return prefix + "/add";
	}
	
	@Log("保存用户")
	@PostMapping("/save")
	@ResponseBody
	Result save(@Validated User user,BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		} 
		
		user.setPassword(passwordEncoder.encode(user.getPassword()));       //使用spring security的加密方式加密密码
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		user.setActiveCode(uuid);
		user.setStatus(0);    //用户状态(0:正常;1:禁用)
		user.setUserType(1);  //管理员标识(0:超级管理员;1:平台管理员;3:用户)
		if (userService.save(user) > 0) {
			return ResultUtil.success();
		}
		return ResultUtil.error(5, "添加用户失败");
	}
	
	@Log("编辑用户")
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		User user = userService.get(id);
		model.addAttribute("user", user);
		List<Role> roles = roleService.list(id);
		model.addAttribute("roles", roles);
		return prefix+"/edit";
	}
	
	@Log("更新用户")
	@PostMapping("/update")
	@ResponseBody
	Result update(User user) {
		
		if (userService.update(user) > 0) {
			return ResultUtil.success();
		}
		return ResultUtil.error(6, "更新用户信息失败！");
	}
	
	
	

}
